<div class="layui-fluid layui-anim febs-anim" id="febs-bookman" lay-title="出版社信息"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="bookman-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">出版社名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="pubname" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">所在地</label>
                    <div class="layui-input-inline">
                      <input type="text" name="pubwhere" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">成立日期</label>
                    <div class="layui-input-inline">
                      <input type="text" name="setuptime" id="pulishing-bookman-setuptime" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="bookman:add,bookman:delete">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="bookmanTable" lay-data="{id: 'bookmanTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="bookman-status">
  {{#
  var status = {
  0 : {title: '未合作', color: 'volcano'},
  1 : {title: '已合作', color: 'green'},
  }[d.status];
  }}
  <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="bookman-option-one" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="bookman:view,bookman:update,bookman:delete">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="edit" shiro:hasPermission="bookman:update">
    <i class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
  <a lay-event="del" shiro:hasPermission="bookman:delete">
    <i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        $view = $('#febs-bookman'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: 'setuptime', type: 'asc'},
        tableIns;

    form.render();

    initTable();

    laydate.render({
      elem: '#pulishing-bookman-setuptime',
      type: 'date'
    });

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('bookmanTable');
        if (name === 'add') {
          febs.modal.open('新增信息', 'publishing/bookman/add', {
            btn: ['提交', '重置'],
            area: $(window).width() <= 750 ? '95%' : '50%',
            yes: function (index, layero) {
              $('#bookman-add').find('#submit').trigger('click');
            },
            btn2: function () {
              $('#bookman-add').find('#reset').trigger('click');
              return false;
            }
          });
        }
        if (name === 'delete') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要删除的记录');
          } else {
            febs.modal.confirm('删除信息', '确定删除该条？', function () {
              var bookmanIds = [];
              layui.each(checkStatus.data, function (key, item) {
                bookmanIds.push(item.id)
              });
              deletebookmans(bookmanIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'add',
        title: '新增出版社',
        perms: 'bookman:add'
      }, {
        name: 'delete',
        title: '删除出版社',
        perms: 'bookman:delete'
      }]
    });
    table.on('tool(bookmanTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        febs.modal.confirm('删除出版社信息', '确定删除该出版社信息？', function () {
          deletebookmans(data.id);
        });
      }
      if (layEvent === 'edit') {
        febs.modal.open('修改出版社信息', 'publishing/bookman/update/' + data.id, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['提交', '取消'],
          yes: function (index, layero) {
            $('#bookman-update').find('#submit').trigger('click');
          },
          btn2: function () {
            layer.closeAll();
          }
        });
      }
    });


    table.on('sort(bookmanTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'bookmanTable',
        url: ctx + 'bookman/list',
        cols: [[
          {type: 'checkbox'},
          {title: '出版社名称', field: 'pubname', minWidth: 100},
          {title: '所在地', field: 'pubwhere'},
          {title: '成立时间', field: 'setuptime'},
          {title: '状态', templet: '#bookman-status'},
          {title: '操作', toolbar: '#bookman-option-one', minWidth: 140}
        ]]
      });
    }

    function getQueryParams() {
      return {
        pubname: $searchForm.find('input[name="pubname"]').val(),
        pubwhere: $searchForm.find('input[name="pubwhere"]').val(),
        setuptime: $searchForm.find('input[name="setuptime"]').val(),
      };
    }

    function deletebookmans(bookmanIds) {
      febs.post(ctx + 'bookman/delete/' + bookmanIds, null, function () {
        febs.alert.success('删除成功');
        $query.click();
      });
    }
  })
</script>